package MediaOnline.WebService;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import MediaOnline.General.paramConst;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

public class DownloadHDHN extends AsyncTask<String, Integer, String> {
		//Dialog the hien tien trinh
		ProgressDialog progress; 
		Context ctx;
		public int idx=0;
		public int i=0;
		public int fileSize=1;
		public String filePath = "/sdcard/Nahi/HDHN";
		String url;
		public DownloadHDHN(Context ctx, String surl) {
			
	        this.ctx = ctx;
	        progress = new ProgressDialog(ctx);
	        url = surl;

	        
	    }
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			Log.d("note::::::", "asynctask.post");
			progress.dismiss();
			
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			
			//Chay progress dialog
			progress.setMessage("Đang tải phim. Vui lòng chờ!!");
			progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progress.setMax(100);
			progress.show();
		}
		
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			progress.setProgress(values[0]);
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			
			try {
	            URL Link = new URL(url);
	            URLConnection connection = Link.openConnection();
	            connection.connect();
	            // this will be useful so that you can show a typical 0-100% progress bar
	            int fileLength = connection.getContentLength();
	            if (fileLength!=0) {
	            	fileSize = fileLength;
				}
	            
	            
	            // download the file
	            InputStream input = new BufferedInputStream(Link.openStream());
	            OutputStream output = new FileOutputStream(filePath,false);

	            byte data[] = new byte[1024];
	            long total = 0;
	            int count;
	            while ((count = input.read(data)) != -1) {
	                total += count;
	                // publishing the progress....
	                i = ((int) (total * 100 / fileLength));
	                publishProgress(i);
	                idx++;
	                //Kiểm tra file đã tồn tại hay chưa
	                File file = new File(filePath);
	                output.write(data, 0, count);
	                
	            }

	            output.flush();
	            output.close();
	            input.close();
	            
	            
	        } catch (Exception e) {
	        	Log.d("error::::", e.toString());
	        }
			return null;
	}
		
		
		
		
}
